package com.xishu.service;

import com.xishu.entity.shop.Catalog;
import com.xishu.entity.shop.CatalogItemMapping;
import com.xishu.entity.shop.Item;
import com.xishu.response.VerifyUtil;
import com.xishu.util.Tools;

import java.util.List;
import java.util.stream.Collectors;

public class CatalogItemService implements Tools {
    private static CatalogItemService instantce = new CatalogItemService();
    private CommonService commonService = CommonService.getInstance();

    private CatalogItemService() {
    }

    public static CatalogItemService getInstantce() {
        return instantce;
    }

    /**
     * 从请求参数里面合成一个映射
     *
     * @param catalogItemMapping
     * @return
     */
    public CatalogItemMapping createCatalogMapping(CatalogItemMapping catalogItemMapping) throws Exception {
        CommonService commonService = CommonService.getInstance();
        ItemService itemService = ItemService.getInstance();
        VerifyUtil.verify(() -> catalogItemMapping.getItemId() != null && catalogItemMapping.getCatalogId() != null);
        Long itemId = catalogItemMapping.getItemId();
        Item item = itemService.findItem(itemId);
        catalogItemMapping.setItemName(item.getName());
        catalogItemMapping.setImgId(item.getImgId());

        Long catalogId = catalogItemMapping.getCatalogId();
        Catalog catalog = commonService.findEntity(catalogId, Catalog.class);
        catalogItemMapping.setCatalogName(catalog.getName());

        return catalogItemMapping;
    }

    /**
     * 同步商品的图片到映射
     *
     * @param itemId
     */
    public void syncItemImgToCatalogItemMapping(Long itemId) throws Exception {
        Item itemInDb = commonService.findEntity(itemId, Item.class);
        List<CatalogItemMapping> catalogItemMappingByItemList = MappingService.getInstance().findCatalogItemMappingByItem(itemId);
        for (CatalogItemMapping catalogItemMapping : catalogItemMappingByItemList) {
            catalogItemMapping.setImgId(itemInDb.getImgId());
            commonService.save(catalogItemMapping);
        }
    }

    /**
     * 过滤合法数据的映射
     *
     * @param catalogItemMappingList
     * @return
     */
    public List<CatalogItemMapping> filterValidateMapping(List<CatalogItemMapping> catalogItemMappingList) {
        List<CatalogItemMapping> itemMappingList = getList(catalogItemMappingList).stream().filter(p -> getLong(p.getItemId()) != 0 && getLong(p.getCatalogId()) != 0).collect(Collectors.toList());
        List<Long> itemIdList = itemMappingList.stream().map(p -> p.getItemId()).collect(Collectors.toList());
        List<Item> itemList = ItemService.getInstance().findItemByItemList(itemIdList)
                .stream().filter(item -> !getBoolean(item.getFlagDelete()))
                .collect(Collectors.toList());

        List<Long> filterItemIdList = itemList.stream().map(item -> item.getId()).collect(Collectors.toList());

        return itemMappingList.stream().filter(p -> filterItemIdList.contains(p.getItemId().longValue())).collect(Collectors.toList());
    }
}
